<template>
  <div class="goods" @scroll="scrollTop($event)">
    <v-header></v-header>
    <div class="goodslist">
      <!-- 搜索区域 -->
      <div class="search-wrap">
        <div class="search">
          <a href="#" class="icon"></a>
          <router-link tag="input" to="/search" type="text" placeholder="点击搜索商品" />
        </div>
      </div>
      <!-- 导航区域 -->
      <ul class="navarea bbt abt" :class="{ fixed: isFixed }">
        <li v-for="(item, index) in navareaList" :key="index">
          <a
            href="javascript:void(0);"
            :class="{ active: index === curBar }"
            :index="index"
            @click="clickHander(index)"
            >
            {{ item }}
             <i 
            v-if="index === 2"
            class="price-sort"
            :style="{backgroundPosition:iconPos}"
            ></i>
            <i 
            v-if="index === 3"
            class="pickOut"
            
            ></i>
            </a>
           
          <div   class="sort-list btop" style="width: 375px" v-if="index === 0 && isShow">
            <div @click="changeSort(i)" :class="{active_sort: i === curSort}" class="rela pad10" v-for="(it,i) in sort_rank" :key="i">
                {{it.item}}
            </div>
          </div>
        </li>
      </ul>
      <!-- 列表 -->
      <ul class="list" v-show="isFilter"  :class="{rea:isShow}">
          <li :class="{mask:isShow}" v-show="isShow" style="height:0px"></li>
        <van-list
          v-model="loading"
          :finished="finished"
          finished-text="已加载全部"
          @load="onLoad"
        >
          <li  class="abt"  v-for="(item, index) in goodsList" :key="index">
              <router-link tag="li" :to="'/goods/' + item.gid">
            <div class="img">
              <div class="coutryimg">
                <img :src="item.country_photo" alt="" />
              </div>
              <img :src="item.photo" alt="" />
                <div v-if="!item.stock" class="sold-out">卖光了</div>

            </div>
          </router-link>
            <div class="info">
              <h1 class="title">
                {{ item.subject }}
              </h1>
              <div class="blank"></div>
              <div class="price">
                <span class="cred">¥{{ item.sale_price }}</span>
                <span class="per">{{ item.dprice }}</span>
              </div>
              <div class="evaluate">
                <span>{{ item.comments }}</span>
                <span class="saled">{{ item.sold }}</span>
                <div class="addcart" @click="addCart(item.gid)">
                  <img
                    src="https://static.epetbar.com/static_wap/epetapp/pages/index/images/addcart.png"
                    alt=""
                  />
                </div>
              </div>
            </div>
          </li>
        </van-list>
      </ul>
      <!-- filter -->
      <goods-filter v-show="!isFilter" @filter="filterHander" :filter-data="filterBoxData" :filter-pp="filterBoxData[0]" :filter-ly="filterBoxData[1]"></goods-filter>
    </div>
    
  </div>
</template>
 
<script>
import vHeader from "../../components/header/header.vue";
import goodsFilter from './goodsFilter.vue';
import Vue from "vue";
import { List, Dialog } from "vant";
Vue.use(List);
// 全局注册
Vue.use(Dialog);
export default {
  data() {
    return {
      cate_id: this.$router.history.current.params.cate_id,
      goodsList: [],
      loading: false,
      finished: false,
      pageNum: 1,
      curBar: 0,
      curSort:0,
      total_page: 0,
      iconPos:'-164px -54px',
      isFixed: false,
      orderby: "def_desc",
      isShow:false,
      isPrice_asc: false,
      navareaList: ["默认", "销量", "价格", "筛选"],
      sortList:['默认','人气','最新','评论'],
      sort_rank:[],
      isFilter:true,
      filterBoxData:[''],
      brandid:'',
      extends_str:'',
    };
  },

  async mounted() {
    await this._loadData();
   
    
  },
  methods: {
    async _loadData() {
      let result = await this.$http.get({
        url: "/goods/list/main.html",
        params: {
          version: 358,
          brandid: 0,
          page: this.pageNum,
          cateid: this.cate_id,
          pet_type: "cat",
          system: "wap",
          isWeb: 1,
          distinct_id:
            "17685498279c38-070e7217d2903e-6d112d7c-1296000-1768549827abe7",
          _: 1609070254794,
        },
      });

      let { list, total_page ,sort_rank,filterBoxData} = result;
      this.sort_rank=sort_rank[0].list;
      this.total_page = total_page;
      this.filterBoxData = filterBoxData;
      this.goodsList = [...this.goodsList, ...list];
   
    },
    async onLoad() {
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      if (this.loading) {
        this.pageNum++;
      }
      await this._loadData();
      this.loading = false;
      // 加载状态结束
      // 数据全部加载完成
      if (this.pageNum >= this.total_page) {
        this.finished = true;
      }
    },
    scrollTop(e) {
      if (e.target.scrollTop >= 96) {
        this.isFixed = true;
      } else {
        this.isFixed = false;
      }
    },
    async _orderData() {
      let result = await this.$http.get({
        url: "/goods/list/main.html",
        params: {
          version: 358,
          page:1,
          brandid: 0,
          cateid: this.cate_id,
          extend_pam: `brandid:${this.brandid}|extends_str:${this.extends_str}|real_wid:|region:`,
          pet_type: "cat",
          system: "wap",
          isWeb: 1,
          orderby: this.orderby,
          distinct_id:
            "17685498279c38-070e7217d2903e-6d112d7c-1296000-1768549827abe7",
          _: 1609245470113,
        },
      });

      let { list } = result;
      this.goodsList = [...list];
    },
    async clickHander(index) {
      this.curBar = index;
      this.iconPos = '-164px -54px';
      if(index!==3&&!this.isFilter){
          this.isFilter = !this.isFilter;
      }
      if (index === 0) {
        this.isShow = !this.isShow;
      }
      if (index === 1) {
        this.orderby = "sold_desc";
         await this._orderData();

      }
      if (index === 2) {
        if (this.isPrice_asc) {
          this.orderby = "price_asc";
          this.isPrice_asc = false;
          this.iconPos = '-113px -54px';
        } else {
          this.orderby = "price_desc";
          this.isPrice_asc = true;
          this.iconPos = '-138px -54px';
        }
          await this._orderData();
      }
      if(index === 3) {
          this.isFilter = !this.isFilter
      }
    },
   async changeSort(index) {
        this.curSort = index;
        if(index===0) {
            this.orderby = "def_desc";
            this.navareaList[0] = this.sortList[0];
        }
        if(index===1) {
            this.orderby = "views_desc";
            this.navareaList[0] = this.sortList[1];
        }
        if(index===2) {
            this.orderby = "lastupdate_desc";
            this.navareaList[0] = this.sortList[2];
        }
        if(index===3) {
            this.orderby = "replys_desc";
            this.navareaList[0] = this.sortList[3];
        }
        this.isShow = false;
       await this._orderData();
        
    },
    filterHander(data) {
        let {list, brandid, extends_str} = data;
        this.brandid = brandid;
        this.extends_str = extends_str;
        this.goodsList = list;
        this.isFilter = true;
        this.finished = true
    },
    // 点击添加购物车 弹出框
    addCart(id) {
        this.$store.commit('addCart',this.goodsList.find(v=>v.gid==id));
        Dialog.confirm({
        message: '商品已经成功加入购物车',
        confirmButtonText:'去购物车',
        cancelButtonText:'继续购物'
        })
        .then(() => {
            // on confirm
            this.$router.push('/cart')
        })
        .catch(() => {
            // on cancel
            Dialog.close()
        });
            }
  },
  components: {
    "v-header": vHeader,
    "goods-filter":goodsFilter
  },
};
</script>
 
<style scoped lang = "stylus">
.pickOut 
    position absolute
    display: inline-block;
    background: url(../../assets/img/brands-ico.png) no-repeat -43px -17px;
    width: 15px;
    height: 14px;
    position: absolute;
    top: 14px;
.price-sort
    display inline-block
    position absolute
    background: url(../../assets/img/brands-ico.png) no-repeat;
    height: 22px;
    width 15px
    top: 10px;
    transform: scale(0.5);
.sold-out
    position absolute 
    top 50%
    left 50%
    transform translate(-50%,-50%)
    width .6rem
    height .6rem
    z-index 20
    background url(../../assets/img/sole-out.png) no-repeat;
    background-size: contain;
.sort-list 
    text-align: left;
    background: #f5f5f5;
    position: relative
    z-index 9999
    div
        line-height: 1.6;
        font-size: 14px;
        padding: 10px;
        text-align: left;
.active_sort 
        background: #fff;
        position: relative;
        &:before
            content: '';
            width: 30px;
            height: 30px;
            display: block;
            background: url(../../assets/img/brands-ico.png) no-repeat -24px -671px;
            position: absolute;
            top: 0;
            right: 0;
.goods 
  display: flex;
  width 100%
  height: 100%;
  flex-direction: column;
  overflow-y: auto;


.list.rea
    position: relative;
    top 1.68rem
.mask
    position absolute !important
    background rgba(0,0,0,.5) !important
    top 0
    let 0
    height 100% !important
    width 100% !important
    z-index 20




.goodslist
  display flex
  flex-direction column
  height 617px
  overflow auto


.search-wrap {
  height: 0.6rem;
  padding: 0.1rem;
  background: #ffffff;

  .search {
    background: #f6f6f6;
    height: 0.4rem;
    border-radius: 0.05px;

    .icon {
      display: inline-block;
      height: 0.2rem;
      width: 0.2rem;
      margin: 0.08rem 0 0 0.04rem;
      background: url('../../assets/img/brands-ico.png') -0.51rem -0.76rem no-repeat;
      background-size: 200px auto;
    }

    input {
      height: 40px;
      border-radius: 3px;
      border: none;
      background: #f6f6f6;
      font-family: 'Microsoft Yahei', tahoma, arial;
      padding: 0px;
      outline: none;
      width: 90%;
      text-indent: 5px;
    }
  }
}

.navarea {
  width: 100%;
  height: 0.41rem;
  display: flex;
  position: relative;

  &>li {
    width: 25%;
    height: 0.41rem;
    text-align: center;
    line-height: 0.41rem;

    a {
      position relative
      display: block;
      height: 0.41rem;
      width: 100%;
      color: #333;  
        
    }

    a.active {
      color: #41C856;
    }
  }
}




.list {
     flex 1
  li {
    position: relative;
    display: flex;
    padding: 0.1rem;
    height: 1.23rem;
    background: #fff;

    .img {
      position: relative;
      width: 1rem;
      height: 1rem;

      &>img {
        display: block;
        width: 100%;
      }

      .coutryimg {
        position: absolute;
        top: 0;
        width: 0.25rem;
        height: 0.25rem;
        border-radius: 0.25rem;
        overflow: hidden;

        img {
          position: absolute;
          top: -0.03rem;
          left: -0.12rem;
          width: 0.5rem;
          height: 0.3rem;
        }
      }
    }

    div.info {
      flex: 1;
      height: 1.03rem;
      padding-left: 0.1rem;

      h1 {
        height: 32px;
        overflow: hidden;
        font-size: 12px;
        line-height: 16px;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
      }

      .blank {
        height: 0.25rem;
        margin-top: 0.05rem;
      }

      .price {
        height: 0.22rem;
        font-size: 0.1rem;
        color: #999;

        .cred {
          color: #FF5757;
          font-size: 14px;
        }

        .per {
          margin-left: 0.1rem;
        }
      }

      .evaluate {
        position: relative;
        height: 0.19rem;
        color: #999;

        .saled {
          margin-left: 0.05rem;
        }
      }
    }
  }
}

.addcart {
  position: absolute;
  bottom: 0;
  right: 0;
  width: 0.3rem;
  height: 0.3rem;

  img {
    width: 100%;
  }
}

.fixed {
  position: sticky;
  top: 0;
  left: 0;
  z-index: 999;
  background: #fff;
}


</style>